package com.catsic.pub.drawer.service.impl;


import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.catsic.base.BaseServiceImpl;
import com.catsic.base.exception.CatsicException;
import com.catsic.base.status.Status;
import com.catsic.base.tree.TreeInfo;
import com.catsic.pub.constants.Constants;
import com.catsic.pub.convert.Converts;
import com.catsic.pub.drawer.dao.DrawerDao;
import com.catsic.pub.drawer.service.DrawerService;
import com.catsic.pub.entity.Drawer;
import com.catsic.pub.exception.CatsicExceptionCode;
import com.catsic.pub.form.DrawerForm;

@Service("drawerService")
public class DrawerServiceImpl extends BaseServiceImpl<Drawer, String> implements
		DrawerService {
	
	@Autowired
	private DrawerDao drawerDao;

	/**
	 * @see com.catsic.pub.drawer.service.DrawerService#add(com.catsic.pub.form.DrawerForm)
	 **/
	@Override
	public DrawerForm add(DrawerForm form){
		Drawer drawer = new Drawer();
		drawer.setName(form.getName());
		drawer.setValue(form.getValue());
		/**
		 * 查询父节点
		 */
		Drawer parent = null;
		if(form.getPid() != null){
			parent = findById(form.getPid());
		}
		drawer.setParent(parent);
		drawer.setCode(code(form.getPid()));
		drawer = super.add(drawer);
		return Converts.convert(drawer);
	}
	
	/**
	 * 生成编码规则
	 * @param pid
	 * @return
	 */
	private String code(String pid){
		/**
		 * 查询父节点下的所有子节点
		 */
		List<Drawer> childs = drawerDao.findByCodeIsNotNullAndParentIdOrderByCodeDesc(pid);
		Drawer parent = null;
		if(StringUtils.isNotBlank(pid)){
			parent = findById(pid);
		}
		if(CollectionUtils.isNotEmpty(childs)){
			String code = childs.get(0).getCode();
			int length = code.length();
			return String.format("%0" + length + "d", Long.valueOf(code)+ 1);
		}else {
			return parent == null ? String.format("%0" + Constants.编码位数 + "d",0) : parent.getCode() + String.format("%0" + Constants.编码位数 + "d",0);
		}
	}
	
	/**
	 * @see com.catsic.core.Drawer.service.DrawerService#update(com.catsic.pub.form.DrawerForm)
	 */
	public DrawerForm update(DrawerForm form){
		Drawer drawer = findById(form.getId());
		drawer.setName(form.getName());
		drawer.setValue(form.getValue());
		drawer = super.add(drawer);
		return Converts.convert(drawer);
	}
	
	/**
	 * @see com.catsic.pub.drawer.service.DrawerService#cancel(java.lang.String)
	 **/
	@Override
	public void cancel(String id){
		Drawer drawer = findById(id);
		drawer.setStatus(Status.无效);
		super.add(drawer);
	}
	
	/**
	 * @see com.catsic.pub.drawer.service.DrawerService#recover(java.lang.String)
	 */
	@Override
	public void recover(String id){
		Drawer drawer = findById(id);
		drawer.setStatus(Status.有效);
		super.add(drawer);
	}
	
	/**
	 * @see com.catsic.pub.drawer.service.DrawerService#active(java.lang.String)
	 */
	@Override
	public void active(String id){
		Drawer drawer = findById(id);
		drawer.setStatus(Status.有效);
		super.add(drawer);
	}

	/**
	 * @see com.catsic.pub.drawer.service.DrawerService#findById(java.lang.String)
	 **/
	@Override
	public Drawer findById(String id) {
		Drawer drawer = getById(id);
		if(null == drawer){
			throw new CatsicException(CatsicExceptionCode.实体不存在, Constants.DRAWER);
		}
		return drawer;
	}
	
	@Override
	public DrawerForm findDrawerById(String id) {
		Drawer drawer = getById(id);
		if(null == drawer){
			throw new CatsicException(CatsicExceptionCode.实体不存在, Constants.DRAWER);
		}
		return Converts.convert(drawer);
	}
	
	/** 
	 * @see com.catsic.pub.drawer.service.DrawerService#findByPid(java.lang.String)
	 */
	public List<Drawer> findByPid(String id){
		return drawerDao.findByPid(id);
	}
	
	/** 
	 * @see com.catsic.pub.drawer.service.DrawerService#getTreeInfo()
	 */
	public List<TreeInfo> getTreeInfo(){
		List<Drawer> list = drawerDao.findAll();
		List<TreeInfo> listForm = new ArrayList<TreeInfo>();
		for(Drawer drawer : list){
			TreeInfo treeInfo = Converts.convertTree(drawer);
			if(StringUtils.equals(treeInfo.getId(), Constants.ENTITY_ID)){
				treeInfo.setOpen(true);
			}
			listForm.add(treeInfo);
		}
		return listForm;
	}
	
	/**
	 * @see com.catsic.pub.drawer.service.DrawerService#query(java.lang.String)
	 */
	public List<DrawerForm> query(String id){
		List<Drawer> list = findByPid(id);
		List<DrawerForm> listForm = new ArrayList<DrawerForm>();
		for(Drawer drawer : list){
			DrawerForm form = Converts.convert(drawer);
			listForm.add(form);
		}
		return listForm;
	}
	
	/**
	 * @see com.catsic.pub.drawer.service.DrawerService#findByParentCode(java.lang.String)
	 **/
	@Override
	public List<DrawerForm> findByParentCode(String code) {
		List<DrawerForm> drawerFormList = new ArrayList<DrawerForm>();
		List<Drawer> list = drawerDao.findByParentCode(code);
		for (Drawer drawer : list) {
			drawerFormList.add(Converts.convert(drawer));
		}
		return drawerFormList;
	}
	
	/**
	 * @see com.catsic.pub.drawer.service.DrawerService#findByCode(java.lang.String)
	 */
	public Drawer findByCode(String code) {
		Drawer drawer = drawerDao.findByCode(code);
		if(null == drawer){
			throw new CatsicException(CatsicExceptionCode.实体不存在, Constants.DRAWER);
		}
		return drawer;
	}
	
	/**
	 * @see com.catsic.pub.drawer.service.DrawerService#findDrawerByCode(java.lang.String)
	 */
	public DrawerForm findDrawerByCode(String code) {
		Drawer drawer = findByCode(code);
		return Converts.convert(drawer);
	}
	
	
	/**
	 * @see com.catsic.pub.drawer.service.DrawerService#findByParentAndValue(java.lang.String,java.lang.String)
	 */
	@Override
	public Drawer findByParentAndValue(String parentCode, String value) {
		return drawerDao.findByParentAndValue(parentCode, value);
	}
}
